package com.xywy.wjk.widget;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.xywy.R;

/**
 * 云健康 - 体重圆盘
 * 
 * @author xgd
 * @version 1.0
 */
public class CircleView extends View {
	private Context mContext;
	private Paint mInnerPaint;
	// 画实心圆的画笔
	private Paint mCirclePaint;
	// 画圆环的画笔
	private Paint mRingPaint;
	// 画字体的画笔
	private Paint mTextPaint;
	// 画字体的画笔 上下文字 60, 0
	private Paint mTextPaint2;
	// 画字体的画笔 line
	private Paint linePaint;

	// 画字体的画笔 测量值差距
	private Paint gapPaint;
	/** 画字体的画笔 测量值差距 */
	private Paint valuePaint;

	// 圆形颜色
	private int mCircleColor;
	// 圆环颜色
	private int mRingColor;
	// 半径
	private float mRadius;
	// 圆环半径
	private float mRingRadius;
	// 圆环宽度
	private float mStrokeWidth;
	// 圆心x坐标
	private float mXCenter;
	// 圆心y坐标
	private float mYCenter;
	// 字的长度
	private float mTxtWidth;
	// 字的高度
	private float mTxtHeight;
	// 字的高度
	private float mTxtHeight2;
	// 健康状况
	private float mTxtHeight3;
	// 总进度
	private float mTotalProgress = 100;
	// 当前进度
	private float mProgress;

	/** 测量值的宽度 */
	private float mValueWidth;
	/** 测量值的高度 */
	private float mValueHeight;
	private float mGapWidth;
	private float mGapHeight;

	private Bitmap mPointer;

	private double pi = 3.1415926;

	private Paint pathPaint;

	private float bCircleR; // 大圆半径
	private float sCircleR; // 小圆半径

	private String diffStr = "";// 跟上次相比 还可以是正在测量 请稍候
	private String healthStr = "";// 健康状况
	private Typeface tf;

	public CircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		// 获取自定义的属性
		initAttrs(context, attrs);
		initVariable();
	}

	private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.CircleView, 0, 0);
		mRingColor = typeArray.getColor(R.styleable.CircleView_ringColor,
				0xFFFFFFFF);
		// 初始背景图
		Resources res = mContext.getResources();
		mPointer = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.zhen1);
		bCircleR = mPointer.getWidth();
		sCircleR = bCircleR / 3;
	}

	private void initVariable() {
		// 圆环的宽度
		// mStrokeWidth = bmp2.getWidth() / 2 - bmp4.getWidth() / 2;
		AssetManager mgr = mContext.getAssets();// 得到AssetManager
		tf = Typeface.createFromAsset(mgr, "hwxh.ttf");// 根据路径得到Typeface
		mStrokeWidth = bCircleR - sCircleR;
		mRingPaint = new Paint();
		mRingPaint.setAntiAlias(true);
		mRingPaint.setColor(mRingColor);
		mRingPaint.setStyle(Paint.Style.STROKE);
		mRingPaint.setStrokeWidth(mStrokeWidth);

		mTextPaint = new Paint();
		mTextPaint.setAntiAlias(true);
		mTextPaint.setStyle(Paint.Style.FILL);
		mTextPaint.setColor(mContext.getResources().getColor(
				R.color.healthConditionColor));
		mTextPaint.setTextSize(dip2px(mContext, 25));
		mTextPaint.setTypeface(tf);

		mTextPaint2 = new Paint();
		mTextPaint2.setAntiAlias(true);
		mTextPaint2.setStyle(Paint.Style.FILL);
		mTextPaint2.setColor(mContext.getResources().getColor(
				R.color.wjk_detail_font2));
		mTextPaint2.setTextSize(30);

		linePaint = new Paint();
		linePaint.setAntiAlias(true);
		linePaint.setStyle(Paint.Style.FILL);
		linePaint.setColor(mContext.getResources().getColor(
				R.color.wjk_yp_line_left));
		linePaint.setStrokeWidth(5);
		// 测量相差数
		gapPaint = new Paint();
		gapPaint.setAntiAlias(true);
		gapPaint.setStyle(Paint.Style.FILL);
		gapPaint.setColor(mContext.getResources().getColor(
				R.color.wjk_detail_font2));
		gapPaint.setTextSize(60);

		valuePaint = new Paint();
		valuePaint.setAntiAlias(true);
		valuePaint.setStyle(Paint.Style.FILL);
		valuePaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
		valuePaint.setColor(mContext.getResources().getColor(
				R.color.measure_value_color));
		valuePaint.setTextSize(dip2px(mContext, 60));

		FontMetrics fm = mTextPaint.getFontMetrics();
		mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent) + 10;

		// 上下60 、0 的高度
		FontMetrics fm2 = mTextPaint2.getFontMetrics();
		mTxtHeight2 = (int) Math.ceil(fm2.descent - fm2.ascent);

		// 上下60 、0 的高度
		FontMetrics fm3 = mRingPaint.getFontMetrics();
		mTxtHeight3 = (int) Math.ceil(fm3.descent - fm3.ascent);
		/** 测量值的高度 */
		FontMetrics fm4 = valuePaint.getFontMetrics();
		mValueHeight = (int) Math.ceil(fm4.descent - fm4.ascent);

		/** 说明测量值相差的字体高度 */
		FontMetrics fm5 = gapPaint.getFontMetrics();
		mGapHeight = (int) Math.ceil(fm5.descent - fm5.ascent);

		pathPaint = new Paint();
		pathPaint.setColor(Color.BLACK);
		pathPaint.setAntiAlias(true);

		mCirclePaint = new Paint();
		mCirclePaint.setColor(0xfff1f1f1);
		mCirclePaint.setStyle(Style.FILL);
		mInnerPaint = new Paint();
		mInnerPaint.setColor(Color.WHITE);
		mInnerPaint.setStyle(Style.FILL);

	}

	@Override
	protected void onDraw(Canvas canvas) {

		mXCenter = getMeasuredWidth() / 2;
		mYCenter = getMeasuredHeight() / 2;

		// 大圆
		canvas.drawCircle(mXCenter, mYCenter, bCircleR, mCirclePaint);
		// 内圆
		canvas.drawCircle(mXCenter, mYCenter, sCircleR, mInnerPaint);

		canvas.drawRect(0, mYCenter, getMeasuredWidth(), getMeasuredHeight(),
				mInnerPaint);

		mRingRadius = mStrokeWidth / 2 + sCircleR;
		Log.e("hahahah", (mProgress / mTotalProgress) * 60 + "");
//		if (mProgress >= 0) {
			RectF oval = new RectF();
			oval.left = (mXCenter - mRingRadius);
			oval.top = (mYCenter - mRingRadius);
			oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
			oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
			Log.e("hehehehehehhe", ((float) mProgress / mTotalProgress) * 60
					+ "");
			canvas.drawArc(oval, 180,
					((float) mProgress / mTotalProgress) * 60, false,
					mRingPaint);
			float degree = ((float) mProgress / mTotalProgress) * 60;
			if (degree < 20)
				degree = degree + 1;
			drawBitMap(canvas, mXCenter, mYCenter, degree);
			// String txt = "健康";
			mTextPaint.setTextSize(dip2px(mContext, 25));
			mTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
			FontMetrics fm = mTextPaint.getFontMetrics();
			mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent) + 10;
			float currHeight = mTxtHeight;
//			Log.e("nulll?? ", healthStr);
			mTxtWidth = mTextPaint
					.measureText(healthStr, 0, healthStr.length());
			canvas.drawText(healthStr, mXCenter - mTxtWidth / 2, mYCenter
					+ mTxtHeight, mTextPaint);

			String mValue = mProgress + "";
			mValueWidth = valuePaint.measureText(mValue, 0, mValue.length())
					+ mTextPaint.measureText("kg", 0, 2);

			canvas.drawText(mValue, mXCenter - mValueWidth / 2, mYCenter
					+ mTxtHeight / 2 + mValueHeight + 10, valuePaint);
			canvas.drawText(
					"kg",
					mXCenter
							- mValueWidth
							/ 2
							+ valuePaint.measureText(mValue, 0, mValue.length()),
					mYCenter + mTxtHeight / 2 + mValueHeight + 10, mTextPaint);

			mTextPaint.setTextSize(dip2px(mContext, 17));
			mTextPaint.setTypeface(tf);
			FontMetrics fm2 = mTextPaint.getFontMetrics();
			mTxtHeight = (int) Math.ceil(fm2.descent - fm2.ascent) + 10;
			float mHealthWidth = mTextPaint.measureText(diffStr, 0,
					diffStr.length());

			canvas.drawText(diffStr, mXCenter - mHealthWidth / 2, mYCenter
					+ mTxtHeight / 2 + mValueHeight + 10 + currHeight,
					mTextPaint);
			if (mProgress == 100) {
				setJkContent(canvas);
			}
//		} else {
//			drawBitMap(canvas, mXCenter, mYCenter, 1);
//		}

	}

	/**
	 * 测量值显示
	 * 
	 * @param canvas
	 */
	public void setJkContent(Canvas canvas) {
		// 显示圆盘中的对比值
		String gap = mContext.getString(R.string.tv_wjk_yp_zk) + "x" + "kg";
		mGapWidth = gapPaint.measureText(gap, 0, gap.length());
		canvas.drawText(gap, mXCenter - mGapWidth / 2, mYCenter + mTxtHeight
				+ mTxtHeight3 + mValueHeight + mGapHeight, gapPaint);
	}

	public void setProgress(float progress, String diff, String healthStr) {
		mProgress = progress;
		this.healthStr = healthStr;
		this.diffStr = diff;
		postInvalidate();
	}

	private Bitmap roteBitmap(float degree) {
		Matrix matrix = new Matrix();
		matrix.setRotate(degree, mPointer.getWidth(), mPointer.getHeight());
		return Bitmap.createBitmap(mPointer, 0, 0, mPointer.getWidth(),
				mPointer.getHeight(), matrix, true);
	}

	private void drawBitMap(Canvas canvas, float centerx, float centery,
			float degree) {
		Paint paint = new Paint();
		Bitmap temp = roteBitmap(degree);
		float x = (float) (centerx - Math.cos((degree / 180) * pi) * bCircleR);
		float y = (float) (centery - Math.sin((degree / 180) * pi) * bCircleR);
		canvas.drawBitmap(temp, x, y, paint);
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}
